<?php

namespace catchAdmin\customs\model;


use catchAdmin\externalGoods\model\GoodsAttribute;
use catchAdmin\externalGoods\model\GoodsRule as externalGoodsRuleModel;
use catcher\base\CatchModel;
use catcher\CatchAuth;


class CnOrder extends CatchModel
{

    protected $name = 'db_customs_cn_order';

    protected $json = [
        'receiverInfo',
        'orderItemList',
        'senderInfo',
        'refunderInfo',
        'orderAmountInfo',
        'customsDeclareInfo',
        'extendFields',
        'departureInfo',
        'logisticsServices',
        'packageInfo',
        '_ext_data'
    ];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    protected $field = [
        'id',
        'push_status',
        'ownerUserId',
        'businessUnitId',
        'externalOrderCode',
        'orderType',
        'storeCode',
        'externalShopId',
        'externalShopName',
        'orderSource',
        'orderSubSource',
        'orderCreateTime',
        'orderPayTime',
        'saleMode',
        'receiverInfo',
        'senderInfo',
        'refunderInfo',
        'orderItemList',
        'orderAmountInfo',
        'customsDeclareInfo',
        'extendFields',
        'remark',
        'serviceId',
        'departureInfo',
        'sellerRemark',
        'buyerRemark',
        'logisticsServices',
        'cnOrderCode',
        'mailNo',
        'partnerCode',
        'partnerName',
        'packageInfo',
        '_ext_data',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id',
    ];

    public function getList($tenant_id = 0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }

        return $this
            ->withoutField('updated_at,deleted_at,tenant_id,creator_id,_ext_data')
            ->catchSearch()
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item) {
                if(!empty($item['packageInfo'])){
//                    $item['packageInfo'] = json_decode($item['packageInfo'],true);
//                    dd($item['packageInfo']);
                    $item['tmsOrders'] = $item['packageInfo']['tmsOrders'];
                    //tmsCode tmsOrderCode
                    $tmsOrders = $item['packageInfo']['tmsOrders'];
                    $mailNoArr = [];

                    foreach ($tmsOrders as $key => $v){
                        $mailNoArr[$key]['tmsCode'] = $v['tmsCode'];
                        $mailNoArr[$key]['tmsOrderCode'] = $v['tmsOrderCode'];
                    }
                    $item['mailNoArr'] = $mailNoArr;
                    unset($item['tmsOrders']);

//                    $tmsCodeArr = array_column($tmsOrders,'tmsCode');
//                    $tmsOrderCodeArr = array_column($tmsOrders,'tmsOrderCode');
//                    $mailNoArr = [];
//
//                    for ($i =0;$i < count($tmsCodeArr);$i++){
//                        $mailNoArr[$i]['tmsCode'] = $tmsCodeArr[$i];
//                        $mailNoArr[$i]['tmsOrderCode'] = $tmsOrderCodeArr[$i];
//                    }
//                    $item['mailNoArr'] = $mailNoArr;
//                    unset($item['tmsOrders']);
                }


            });
    }

    //获取支付的代码
    public function getPayTypeAttr($value,$data)
    {
        $status = ["XSZF"=>"17","ALIPAY"=>"01","WEIXINPAY"=>"02"];
        return $status[$data['customsDeclareInfo']['payChannel']];
    }
}